package com.sogoodsoft.web.sys;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.app.SysClass;
import com.sogoodsoft.service.app.SysClassService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.web.CrudActionSupport;
import com.sogoodsoft.web.WebConstants;

@Namespace("/sys")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "sys-type.action", type = "redirect")})
public class SysTypeAction extends CrudActionSupport<SysClass> {
	
	private static final long serialVersionUID = -4052047494894591406L;

	private SysClassService sysClassSer;
	
	private Long id;
	private SysClass sysClass;
	
	private List<SysClass> tree;
	
	private Long parentId;
	private SysClass parent;
	
	private Page<SysClass> page = new Page<SysClass>(WebConstants.PAGE_SIZE);//每页5条记录
	
	@Override
	public String delete() throws Exception {
		sysClass = new SysClass();
		sysClass.setId(id);
		sysClassSer.delete(sysClass);
		Struts2Utils.renderJson(DwzMsg.success());
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("odr");
			page.setOrder(Page.ASC);
		}
		
		logger.debug("查询系统分类");
		sysClassSer.findPage(page,filters);
		return SUCCESS;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id!=null){
			sysClass = sysClassSer.get(id);
		}else{
			sysClass = new SysClass();
		}
		
		if(parentId!=null){
			parent = sysClassSer.get(parentId);
			sysClass.setParent(parent);
		}
	}

	@Override
	public String save() throws Exception {
		sysClassSer.save(sysClass);
		Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils.getParameter(DwzMsg.param_navTabId)));
		return null;
	}

	@Override
	public SysClass getModel() {
		// TODO Auto-generated method stub
		
		return sysClass;
	}
	
	public String jstree(){
		logger.info("输出jsTree");
		if(id!=null){
			tree = sysClass.composite();
		}else{
			tree = sysClassSer.getFirst().composite();
		}
		
		return "JSTREE";
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public SysClass getSysClass() {
		return sysClass;
	}

	public void setSysClass(SysClass sysClass) {
		this.sysClass = sysClass;
	}

	public List<SysClass> getTree() {
		if(tree==null){
			jstree();
		}
		return tree;
	}

	public void setTree(List<SysClass> tree) {
		this.tree = tree;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public SysClass getParent() {
		return parent;
	}

	public void setParent(SysClass parent) {
		this.parent = parent;
	}

	public Page<SysClass> getPage() {
		return page;
	}

	public void setPage(Page<SysClass> page) {
		this.page = page;
	}

	public SysClassService getSysClassSer() {
		return sysClassSer;
	}

	@Autowired
	public void setSysClassSer(SysClassService sysClassSer) {
		this.sysClassSer = sysClassSer;
	}


	
}
